import dayjs from "@/plugin/dayjs/dayjs.min.js";

var dateDto = {};


/**
 * 格式化日期字符串
 * @param {date} 日期  
 * @param {formatStr} 格式字符串
 */
export function format(date, formatStr) {
	if (formatStr == undefined) {
		formatStr = "YYYY-MM-DD";
	}
	return dayjs(date).format(formatStr);
}

/**
 * 时间相对目前  几秒钟前 几分钟前
 * @param {date} 日期  
 */
export function fromNow(date) {
	let dateJs = dayjs(date);
	let year = dateJs.year();
	let month = dateJs.month();
	let day = dateJs.day();

	var current = dayjs();
	let currentYear = current.year();
	let currentMonth = current.month();
	let currentDay = current.month();
	if (year == currentYear) { //当年
		if (currentMonth == month) { //当月
			//日期插件
			dayjs.extend(relativeTime)
			var infoStr = dayjs(date).fromNow();
			return infoStr
		} else {
			return dateDto.format(date, "MM-DD");
		}
	} else {
		return dateDto.format(date, "YYYY-MM-DD");
	}
}

/**
 * 时间差值
 * @param {dateF} 起止日期 
 * @param {dateE} 结束日期   
 */

export function diff(dateF, dateE) {
	let dateJsF = dayjs(dateF);
	let dateJsE = dayjs(dateE);

	let month = dateJsF.diff(dateJsE, 'day'); //年份
	return Math.abs(month) + '天';
}


//15分钟距离现在的差值
export function diffMinus(dateF) {
	let dateJsF = dayjs(dateF);
	dateJsF = dateJsF.add(15, 'm');
	
	let dateJsE = dayjs(new Date());
	let second = dateJsF.diff(dateJsE, 'second'); //年份
	return second ;
}

//获取当年的第一天
export function getYear(date) {
	let year = dayjs(date).year();
	return year;
}

/**
 days	d
weeks	w
months	M
years	y
hours	h
minutes	m
seconds	s
milliseconds	ms
 */
export function addMinute(dateF, dayNum) {
	let dateJsF = dayjs(dateF);
	let formatStr = "YYYY-MM-DD";
	return dateJsF.add(dayNum, 'm').format(formatStr);
}
export function addHours(dateF, dayNum) {
	let dateJsF = dayjs(dateF);
	let formatStr = "YYYY-MM-DD";
	return dateJsF.add(dayNum, 'h').format(formatStr);
}
export function addWeek(dateF, dayNum) {
	let dateJsF = dayjs(dateF);
	let formatStr = "YYYY-MM-DD";
	return dateJsF.add(dayNum, 'w').format(formatStr);
}

export function addDay(dateF, dayNum) {
	let dateJsF = dayjs(dateF);
	let formatStr = "YYYY-MM-DD";
	return dateJsF.add(dayNum, 'd').format(formatStr);
}

export function addMouth(dateF, dayNum) {
	let dateJsF = dayjs(dateF);
	let formatStr = "YYYY-MM-DD";
	return dateJsF.add(dayNum, 'M').format(formatStr);
}

export function addYear(dateF, dayNum) {
	let dateJsF = dayjs(dateF);
	let formatStr = "YYYY-MM-DD";
	return dateJsF.add(dayNum, 'y').format(formatStr);
}


export function isBefore(date) {
	let isBefore = dayjs().isBefore(dayjs(date));
	return isBefore;
}


